package com.kyxx.topics.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kyxx.topics.BaseActivity;
import com.kyxx.topics.BaseApp;
import com.kyxx.topics.R;
import com.kyxx.topics.adapter.TioOffAdapter;
import com.kyxx.topics.bean.TipOffBean;
import com.kyxx.topics.net.LoadingStringCallBack;
import com.kyxx.topics.net.OkGoRequest;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.ScreenSizeUtils;
import com.kyxx.topics.utils.ToastUtils;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class TipOffActivity extends BaseActivity {

    @Bind(R.id.iv_tip_off_back)
    ImageView iv_tip_off_back;
    @Bind(R.id.rv_tip_off)
    RecyclerView rv_tip_off;
    @Bind(R.id.tv_tip_off_info)
    TextView tv_tip_off_info;
    @Bind(R.id.tv_tip_off_click)
    TextView tv_tip_off_click;
    @Bind(R.id.rl_tip_off_click)
    RelativeLayout rl_tip_off_click;
    @Bind(R.id.rl_baoliao)
    RelativeLayout rl_baoliao;
    @Bind(R.id.tv_cancel)
    TextView tvCancel;
    @Bind(R.id.tv_tip_off_baoliao)
    TextView tv_tip_off_baoliao;
    private TioOffAdapter adapter;
    private List<TipOffBean> bean;
    private final static int NOTIFY = 1;
    private final static int NEW = 2;

    protected void findViewById() {
    }

    protected void loadViewLayout() {
        setContentView(R.layout.activity_tip_off);
        ButterKnife.bind(this);
        bean = new ArrayList<>();
    }


    protected void processLogic() {
        rv_tip_off.setLayoutManager(new LinearLayoutManager(this));
        rv_tip_off.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if(bean.get(i).getStatus() == 1){
                    startActivity(new Intent(context,DetailActivity.class).putExtra("id",bean.get(i).getId()+"").putExtra("title",bean.get(i).getTitle()));
                }

            }
        });
        getBID();
    }

    protected void setListener() {
    }


    @OnClick({R.id.iv_tip_off_back, R.id.tv_tip_off_info, R.id.tv_tip_off_click,R.id.tv_tip_off_baoliao})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_tip_off_back:
                finish();
                break;
            case R.id.tv_tip_off_info:
                dialog();
                break;
            case R.id.tv_tip_off_click:
                startActivity(TipOffSubActivity.class);
                break;
            case R.id.tv_tip_off_baoliao:
                startActivity(TipOffSubActivity.class);
                break;
        }
    }

    private void dialog() {
        final Dialog dialog = new Dialog(TipOffActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View mDialogView = View.inflate(TipOffActivity.this, R.layout.dialog_tip_off, null);
        TextView tv = (TextView) mDialogView.findViewById(R.id.tv_cancel);
        mDialogView.setBackgroundColor(getResources().getColor(R.color.white));
        mDialogView.setMinimumHeight((int) (ScreenSizeUtils.getInstance(TipOffActivity.this).getScreenHeight() * 0.5f));
        dialog.setContentView(mDialogView);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (ScreenSizeUtils.getInstance(TipOffActivity.this).getScreenWidth() * 0.83f);
        lp.height = (int) (ScreenSizeUtils.getInstance(TipOffActivity.this).getScreenHeight() * 0.5f);
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
        dialog.show();
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }

    private void getBID() {
        HttpParams params = new HttpParams();
        JSONObject obj = new JSONObject();
        obj.put("uid", BaseApp.getModel().getId());
        params.put("json", obj.toString());
        OkGoRequest.post(UrlUtils.GET_BID, context, "BID", CacheMode.REQUEST_FAILED_READ_CACHE, params, new LoadingStringCallBack(TipOffActivity.this,true) {
            @Override
            public void onCacheSuccess(String s, Call call) {
                super.onCacheSuccess(s, call);
                onSuccess(s, call, null);
            }

            @Override
            public void onSuccess(String s, Call call, Response response) {
                try {
                    org.json.JSONObject result = new org.json.JSONObject(s);
                    if (result.getInt("code") == 0) {
                        bean = JSONObject.parseArray(result.getJSONArray("data").toString(), TipOffBean.class);
                        if (bean.size() > 0) {
                            mHandler.sendEmptyMessage(NOTIFY);
                        } else {
                            mHandler.sendEmptyMessage(NEW);
                        }
                    } else {
                        ToastUtils.show(context, result.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                ToastUtils.show(context, "网络错误请稍后再试");
            }
        });
    }

    Handler mHandler = new Handler() {
        @Override
        public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
            switch (msg.what) {
                case NOTIFY:
                    rv_tip_off.setVisibility(View.VISIBLE);
                    rl_tip_off_click.setVisibility(View.GONE);
                    tv_tip_off_baoliao.setVisibility(View.VISIBLE);
                    adapter = new TioOffAdapter(bean);
                    rv_tip_off.setAdapter(adapter);
                    break;
                case NEW:
                    rv_tip_off.setVisibility(View.GONE);
                    rl_tip_off_click.setVisibility(View.VISIBLE);
                    tv_tip_off_baoliao.setVisibility(View.GONE);
                    break;
            }
            return super.sendMessageAtTime(msg, uptimeMillis);
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        getBID();
    }
}